package com.sunfeng.netty.atguigu.codec4;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.TextFormat;

/**
 * Protobuf编解码开发
 *
 * @author sunfeng
 * @date 2021/05/19 16:35
 **/
public class TestSubScribeReqProto {

    private static byte[] encode(SubScribeReqProto.SubScribeReq subScribeReq) throws InvalidProtocolBufferException {
        return subScribeReq.toByteArray();
    }



    private static SubScribeReqProto.SubScribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubScribeReqProto.SubScribeReq.parseFrom(body);
    }


    private static SubScribeReqProto.SubScribeReq createSubscribeReq() {
        SubScribeReqProto.SubScribeReq.Builder builder = SubScribeReqProto.SubScribeReq.newBuilder();
        builder.setSubReqID(1);
        builder.setUserName("sunfeng");
        builder.setProductName("Netty 实战");
        builder.setAddress("山东菏泽曹县");
        builder.setPhoneNumber("18354680214");
        return builder.build();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubScribeReqProto.SubScribeReq subscribeReq = createSubscribeReq();
        //TextFormat.printToUnicodeString 解决中文输出乱码问题
        System.out.println("编码前："+TextFormat.printToUnicodeString(subscribeReq));

        SubScribeReqProto.SubScribeReq decode = decode(encode(subscribeReq));
        System.out.println("解码后："+TextFormat.printToUnicodeString(subscribeReq));

        System.out.println(decode.equals(subscribeReq));


    }
}
